////////////////////////////////////////////////////////////////////////////////
//
//  Copyright
//
////////////////////////////////////////////////////////////////////////////////

package goodcoregame.display.gfx.character {

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.describeType;
import flash.utils.getClassName;

import goodcoregame.database.character.CharacterData;

import ru.goodcore.resource.ResourceManager;
import ru.goodcore.resource.ResourceVault;
import ru.goodcore.resource.getResourceManager;
import ru.goodcore.utils.StringUtils;

/**
 * @author					Dimarik
 * @version					1.0
 * @playerversion			Flash 9
 * @langversion				3.0
 */
public class Character extends Sprite {

	//-------------------------------------------------------------------------
	//
	//  Class variables
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 * resource manager 
	 */	
	private const _resourceManager:	ResourceManager = getResourceManager();

	//-------------------------------------------------------------------------
	//
	//  Class methods
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//  Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 */
	public function Character(data:CharacterData) {
		super();
		this.data = data;
	}

	//-------------------------------------------------------------------------
	//
	//  Properties
	//
	//-------------------------------------------------------------------------

	//---------------------------------
	//  data
	//---------------------------------
	/**
	 * @private
	 */
	private var _data:	CharacterData;

	public function get data():CharacterData {
		return this._data;
	}

	/**
	 * @private 
	 * @param value
	 */	
	public function set data(value:CharacterData):void {
		if (this._data == value) return;

		// TODO отписка прежней модели от событий
		if (this._data) {
			
		}

		this._data = value;

		// TODO подписка модели на события
		if (this._data) {
			this.update();
		} else {
			this.destroy();
		}

	}

	//-------------------------------------------------------------------------
	//
	//  Private variables
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//  Public methods
	//
	//-------------------------------------------------------------------------

	 public function destroy():void {
		super.removeEventListener(Event.ADDED_TO_STAGE, this.handler_addedToStage);
		this._data = null;

		//super.destroy();
	}

	//-------------------------------------------------------------------------
	//
	//  Protected methods
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//  Private methods
	//
	//-------------------------------------------------------------------------

	/**
	 * @private 
	 * 
	 */	
	private function update():void {
		// Соберем сигнатуру текущего визуального состояния
		var resourceSignature:String = 
				this._data.race + '_' +
				StringUtils.setNumberLen(this._data.action, 2, 16) + 
				StringUtils.setNumberLen(this._data.rotation, 2, 36);

		//trace('update: ' + resourceSignature);

		var rv:ResourceVault = _resourceManager.getResourceVault(resourceSignature);

		if ( !rv.ready )
			rv.addEventListener(Event.COMPLETE, this.handler_resourceVaultReady);
		else 
			this.render(rv);
	}

	private function render(rv:ResourceVault):void {
		var className:String = 
			StringUtils.setNumberLen(this._data.action, 2, 16) + 
			StringUtils.setNumberLen(this._data.rotation, 2, 36);

		var c:Class = rv.getResource(className) as Class;

		var mc:MovieClip = MovieClip( new c() );

		super.addChild(mc);

		mc.play();
	}

	//-------------------------------------------------------------------------
	//
	//  Event handlers
	//
	//-------------------------------------------------------------------------

	/**
	 * @private 
	 * @param event
	 */
	private function handler_addedToStage(event:Event):void {
		//trace(getClassName(this) + '; handler_addedToStage');
	}

	/**
	 * @private 
	 * @param event
	 */
	private function handler_resourceVaultReady(event:Event):void {
		//trace(getClassName(this) + '; handler_resourceValueReady');
		this.update();
	}
}
}